package com.qdxwx.manage.controller.user;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.order.OrderService;
import com.qdxwx.data.service.user.UserLetterService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.LetterTemplate;
import com.qdxwx.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户(前台用户)Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/user/course")
public class UserCourseController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    /***
     * 赠送用户课程
     */
    @GetMapping("/list.html")
    public String courseList(HttpServletRequest request, ModelMap mmap) {
        mmap.put("userId", TKConvert.toLong(request.getParameter("userId")));
        return "user/course-give-list";
    }

    /***
     * 赠送用户课程列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo coursesList(HttpServletRequest request) {
        CourseQuery query = new CourseQuery();
        query.setIsAvailable(1);
        query.setCourseName(TKString.queryKey(request.getParameter("courseName")));
        query.setSellTypeExclude("PACKAGE");
        List<Course> list = courseService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(query));
        return rspData;
    }

    /**
     * 赠送课程
     */
    @RequestMapping("/free.json")
    @ResponseBody
    public AjaxResult toFreeCourse(HttpServletRequest request) {
        long userId = TKConvert.toLong(request.getParameter("userId"));
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if (userId < 1 || courseId < 1) {
            return AjaxResult.error("参数错误");
        }
        Course course = courseService.get(courseId);
        if (course == null) {
            return AjaxResult.error("此课程不存在");
        }
        //如果课程价格为0可以观看
        if (course.getPrice() <= 0) {
            return AjaxResult.error("赠送课程价格不能为零");
        }
        User user = userService.get(userId);
        if (user == null) {
            return AjaxResult.error("找不到对应的用户");
        }
        try {
            orderService.addFreeCourse(user, course);
            //发送站内信
            userLetterService.send(userId, LetterTemplate.COURSE_GIVE, course.getCourseName());
            return AjaxResult.success("赠送成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
